package com.markerhub.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.HealthKnowledge;
import com.markerhub.entity.HealthKnowledgeExcel;
import com.markerhub.excel.HealthKnowledgeListener;
import com.markerhub.mapper.SysDictValueMapper;
import com.markerhub.service.HealthKnowledgeService;
import com.markerhub.service.SysDictValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author jsx on 2023/1/27
 **/
@RestController
@Slf4j
@RequestMapping("/health/knowledge")
public class HealthKnowledgeController extends  BaseController {

    //查询所有页面
    @Autowired
    private HealthKnowledgeService healthKnowledgeService;
    @Autowired
    private SysDictValueService sysDictValueService;

    @GetMapping("/value/list")
    public Result list(@RequestParam(required = false) String food ,@RequestParam(required = false) String type , Integer current,Integer size){
        //查询所有的数据
       Page<HealthKnowledge> list = healthKnowledgeService.getlist(getPage(current,size),food,type);
       System.out.println(list.getRecords());
        return Result.succ(list);
    }

    //创建操作
    @PostMapping("/save")
    public Result save(@RequestBody HealthKnowledge healthKnowledge){
        healthKnowledgeService.save(healthKnowledge);
        return Result.succ("创建成功");
    }

    //更新操作
    @PostMapping("/update")
    public Result update(@RequestBody HealthKnowledge healthKnowledge){
        healthKnowledgeService.updateById(healthKnowledge);
        return Result.succ("更新成功");
    }

    //删除操作
    @GetMapping("/delete")
    public Result delete(String id){
        healthKnowledgeService.removeById(id);
        return Result.succ("删除成功");
    }

    //查询单个记录
    @GetMapping("/info/{id}")
    public Result getInfo(@PathVariable("id") String id){
        HealthKnowledge healthKnowledge = healthKnowledgeService.getById(id);
        return Result.succ(healthKnowledge);
    }

    //上传文件
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("/load")
    public Result load(MultipartFile file){
        if(file == null){
            return Result.fail("请先上传文件");
        }
        try {
            EasyExcel.read(file.getInputStream(), HealthKnowledgeExcel.class, new HealthKnowledgeListener
                    (sysDictValueService,healthKnowledgeService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("上传失败");
        }
        return Result.succ("上传成功");
    }

    //所有数据
    @RequestMapping("/allList")
    public Result allList(){
        List<HealthKnowledge> all = this.healthKnowledgeService.getAll();
        return Result.succ(all);
    }

    //模板下载
    @GetMapping("/download")
    public void Download(HttpServletResponse response){
        try {

            // 以流的形式下载文件。
            String filename = "营养膳食知识模板";
            InputStream fis = FoodController.class.getResourceAsStream("/excel/营养膳食知识模板.xlsx");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename + ".xlsx",  "UTF-8"));
            response.addHeader("Content-Length", "" + buffer.length);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.addHeader(  "Access-Control-Allow-Origin","*");//允许所有来源访同
            response.addHeader(  "Access-Control-Allow-Method","POST,GET");//允许访问的方式

            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            log.error("系统有误");
        }

    }
}
